package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServiceResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    private ICartService iCartService;

    /**
     * 查询购物车
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> list(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectList(user.getId());
    }

    /**
     * 加入购物车
     *
     * @param session
     * @param productId
     * @param count
     * @return
     */
    @RequestMapping(value = "addProduct.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> addProduct(HttpSession session, Integer productId, Integer count) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.add(user.getId(), productId, count);
    }

    /**
     * 更新购物车
     *
     * @param session
     * @param productId
     * @param count
     * @return
     */
    @RequestMapping(value = "updateProduce.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> updateProduce(HttpSession session, Integer productId, Integer count) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.update(user.getId(), productId, count);
    }

    @RequestMapping(value = "deleteProduct.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> deleteProduct(HttpSession session, String productIds) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.delete(user.getId(), productIds);
    }

    /**
     * 全选
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "selectAll.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> selectAll(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectAllOrUnSelect(user.getId(), null, Const.Cart.CHECK);
    }


    /**
     * 全反选
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "unSelectAll.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> unSelectAll(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectAllOrUnSelect(user.getId(), null, Const.Cart.UN_CHECK);
    }

    /**
     * 单选
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "Select.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> select(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectAllOrUnSelect(user.getId(), productId, Const.Cart.CHECK);
    }

    /**
     * 单反选
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "unSelect.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<CartVo> unSelect(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectAllOrUnSelect(user.getId(), productId, Const.Cart.UN_CHECK);
    }

    /**
     * 获取当前用户购物车商品数量
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "getCartProductCount.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<Integer> getCartProductCount(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createBySuccess(0);
        }
        return iCartService.getCartProductCount(user.getId());
    }
}
